#include <cstring>
#include "TString.h"
using std::cin;
using std::cout;

int TString::num_strings = 0;
int TString::HowMany()
{
    return num_strings;
}

TString::TString(const char *s)
{
    //cout<<"constructor a new string from char point"<<std::endl;
    len = std::strlen(s);
    str = new char[len+1];
    std::strcpy(str, s);
    num_strings++;
}

TString::TString()
{
    len = 4;
    str = new char[1];
    str[0] = '\0';
    num_strings++;
}

TString::TString(const TString & st)
{
    //cout<<"construct & \n";
    num_strings++;
    len = st.len;
    str = new char[len+1];
    std::strcpy(str, st.str);
}
TString::~TString()
{
    //cout<<"delete function\n";
    --num_strings;
    delete [] str;
}

TString & TString::operator=(const TString &st)
{
    //cout<<"= operator\n";
    if (this == &st){
        return *this;
    }
    delete []str;
    len = st.len;
    str = new char[len+1];
    std::strcpy(str, st.str);
    return *this;
}

TString & TString::operator=(const char *s)
{
    delete [] str;
    len = std::strlen(s);
    str = new char[len+1];
    std::strcpy(str, s);
    return *this;
}

char & TString::operator[](int i)
{
    return str[i];
}

const char & TString::operator[](int i) const
{
    return str[i];
}

bool operator<(const TString &st1, const TString &st2){
    return (std::strcmp(st1.str, st2.str)<0);
}

bool operator>(const TString &st1, const TString &st2){
    return st2<st1;
}

bool operator==(const TString &st1, const TString &st2){
    return (std::strcmp(st1.str, st2.str)==0);
}

ostream & operator<<(ostream &os, const TString &st)
{
    os<<st.str;
    return os;
}

istream & operator>>(istream & is,TString &st){
    char temp[TString::CINLIM];
    is.get(temp, TString::CINLIM);
    if(is) st = temp;
    while(is && is.get()!='\n'){
        continue;
    }
    return is;
}
